package com.jackrain.nea.oms.mq;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

import static com.aliyun.openservices.ons.api.Action.CommitMessage;
import static com.aliyun.openservices.ons.api.Action.ReconsumeLater;

/**
 * Created by nathan on 2017/5/23.
 */
@Slf4j
public class MQMessageListener implements MessageListener {
    @Override
    public Action consume(Message message, ConsumeContext context) {

        try {
            ValueHolder valueHolder = mqConsume(message, context);
            return CommitMessage;
        } catch (Exception e) {
            e.printStackTrace();
            return ReconsumeLater;
        }

    }

    public ValueHolder mqConsume(Message message, ConsumeContext context) throws Exception {


        Serializable ser = null;
        ser = MsgConvertUtil.objectDeserialize(message.getBody());

        log.debug("mqConsume ser:" + ser.toString());


        if (ser instanceof JSONArray) {
            JSONArray paramArray = JSONArray.parseArray(ser.toString());
            log.debug("paramArray:" + paramArray.toJSONString());
        }else if(ser instanceof JSONObject){
            JSONObject paramObject = JSONObject.parseObject(ser.toString());
            log.debug("paramObject:" + paramObject.toJSONString());
        }

        ValueHolder valueHolder = new ValueHolder();

        return valueHolder;


    }
}
